<?php

namespace App\Http\Controllers\Admin;
header('Content-Type:text/html;charset=utf-8');


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Route,URL,Auth;


class JqController extends Controller
{
    public function messages($table){
        if($table == 'add'){
            return [
                'jqbt.required' => '上报标题不能为空',
                'sbren.required'  => '上报人不能为空',
                'lxfs.required' => '联系电话不能为空',
                'jqlx.required' => '警情类型不能为空',
                'cont.required' => '警情内容不能为空',
            ];
        }
        if($table == 'edit'){
            return [
                'cldz.required' => '县市区、派出所不能为空',
                'cldw.required'  => '处理单位不能为空',
                'clry.required' => '处理人不能为空',
                'cldh.required' => '联系电话不能为空',
                'zlqk.required' => '处置情况不能为空',
            ];
        }
    }

	//警情上报
	public function index(Request $request){
		//获取该企业的上报

		if($request->rows) {

			$userid['unit_id'] = Auth::guard('admin')->user()->unit_id;
			$res = DB::table('jqsb')->where($userid)->orderBy('jq_id', 'DESC')->paginate($request->rows);
			$list = $res->items();

            foreach ($list as $k=>$v){
                if($v->police_cl == 0){
                     $list[$k]->police_cl = '未处理';
                }else{
                     $list[$k]->police_cl = '已处理';
                }
            }


			$result['total'] = $res->total();
	        $result['rows'] = $list;
	        echo json_encode($result);

		}else{

		    return view('admin.jq.index');
		}
		
	}

    public function addjq(Request $request){
            if($request->isMethod('post')){
                $this->validate($request, [
                    'jqbt' => 'required',
                    'sbren' => 'required',
                    'lxfs' => 'required',
                    'jqlx' => 'required',
                    'cont' => 'required',
                ],$this->messages('add'));
                $input = $request->all();
                $data = Input::except('_token');
                $unit_info = db::table('units')->where('id',Auth::guard('admin')->user()->unit_id)->first();
                $data = array(
                    'jqbt' => $input['jqbt'],
                    'sbren' => $input['sbren'],
                    'lxfs' => $input['lxfs'],
                    'cont' => $input['cont'],
                    'sbsj' => date('Y-m-d H:i:s',time()),
                    'jqlx' => $input['jqlx'],
                    'unit_id' =>  Auth::guard('admin')->user()->unit_id,
                    'created_at' => date('Y-m-d H:i:s',time()),
                    'city' => $unit_info->city,
                    'area' => $unit_info->area,
                    'org_id' => $unit_info->org_id,
                    'unit_name' => $unit_info->name,
                );
                foreach ($data as $k=>$v){
                    $data[$k] = htmlspecialchars($v);
                }
                $re = DB::table('jqsb')->insert($data);
                if ($re) {
                     return ['status'=>1,'msg'=>'添加成功'];
                }else{
                     return ['status'=>0,'msg'=>'添加失败'];
                }
                // return redirect('admin/bg/bgindex');
            }

            //获取该企业的信息
            
            $map['id'] = Auth::guard('admin')->user()->unit_id;
            $units = DB::table('units')->where($map)->first();
            return view('admin.jq.addjq',['units'=>$units]);
        }

    //修改
    public function jqedit($id,Request $request){
        $re = DB::table('jqsb')->where('jq_id',$id)->first();
        if($request->isMethod('post')){
            $this->validate($request, [
                'cldz' => 'required',
                'cldw' => 'required',
                'clry' => 'required',
                'cldh' => 'required',
                'zlqk' => 'required',
            ],$this->messages('edit'));
            $input = $request->all();
            $data = Input::except('_token');
                 
            $datas = array(
                'czsj' => date('Y-m-d H:i:s',time()),
                'cldz' => $input['cldz'],
                'cldw' => $input['cldw'],
                'clry' => $input['clry'],
                'cldh' => $input['cldh'],
                'zlqk' => $input['zlqk'],
                'police_id' =>  Auth::guard('admin')->user()->id,
                'update_at' => date('Y-m-d H:i:s',time()),
                'police_cl' => '1',
                'chuli_time' => time()-strtotime($re->created_at)
            );
            foreach ($datas as $k=>$v){
                $datas[$k] = htmlspecialchars($v);
            }
            $upjq = DB::table('jqsb')->where('jq_id',$id)->update($datas);

            if ($upjq) {
                 return ['status'=>1,'msg'=>'保存成功'];
            }else{
                 return ['status'=>0,'msg'=>'保存失败'];
            }
        }
        //获取相关信息




        if (Auth::guard('admin')->user()->role_id == 22) {
            $map['id'] = Auth::guard('admin')->user()->unit_id;
        }else{
             $map['id'] = $re->unit_id;
        }
        
        $units = DB::table('units')->where($map)->first();



        return view('admin.jq.jqedit', ['id'=>$id,'re'=>$re,'units'=>$units]);

    }

    //删除警情上报
    public function del($id,Request $request){

        $re = db::table('jqsb')->where('jq_id',$id)->delete();
        if($re){
            return ['status'=>1,'msg'=>'删除成功'];
        }else{
            return ['status'=>0,'msg'=>'删除失败'];
        }
    }

    //民警获取企业的警情资料
    public function police($id,Request $request){
        //获取该企业的上报

        if($request->rows) {

            $userid['unit_id'] = $id;
            $res = DB::table('jqsb')->where($userid)->orderBy('jq_id', 'DESC')->paginate($request->rows);
            $list = $res->items();

            foreach ($list as $k=>$v){
                if($v->police_cl == 0){
                     $list[$k]->police_cl = '未处理';
                }else{
                     $list[$k]->police_cl = '已处理';
                }
            }


            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);

        }else{

            return view('admin.jq.police',['id'=>$id]);
        }
        
    }

     public function wukuang($id,Request $request){
        
        //获取相关信息


        $re = DB::table('jqsb')->where('jq_id',$id)->first();


        if (Auth::guard('admin')->user()->role_id == 22) {
            $map['id'] = Auth::guard('admin')->user()->unit_id;
        }else{
             $map['id'] = $re->unit_id;
        }
        
        $units = DB::table('units')->where($map)->first();

        return view('admin.jq.wukuang', ['id'=>$id,'re'=>$re,'units'=>$units]);

    }






}